package com.bootdo.clouddo.utils.common;

import com.bootdo.clouddo.utils.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtils {
	private static final String PASS_WORD="^([a-z]|[A-Z]|[0-9]){6,50}$";
	private static final String MOBILE="^((13[0-9])|(15[0-9])|(14[0-9])|(17[0-9])|(18[0-9])|(16[0-9])|(19[0-9]))\\d{8}$";
	private static final String NICK_NAME = "^([a-zA-Z0-9\u4e00-\u9fa5]){2,20}$";//英文+中文+数字，2-20位
	private static final String USER_NAME = "^([a-zA-Z0-9\u4e00-\u9fa5]){4,20}$";//英文+中文+数字，4-20位
	/**
	 * 获取3个随机数
	 * @return
	 */
	public static int buildRandom(){
		return (int)((Math.random()*9+1)*100);
	}
	/**
	 * 获取8个随机数
	 * @return
	 */
	public static int build8Random(){
		return (int)((Math.random()*9+1)*10000000);
	}
	/**
	 * 正则匹配密码
	 * @param password
	 * @return
	 */
	public static boolean matchPassword(String password){
		Pattern p = Pattern.compile(PASS_WORD);  
		Matcher m = p.matcher(password);  
		return m.matches();
	}
	
	/**
	 * 验证用户名是否合法
	 * 非法： 纯数字或有特殊字符
	 * @param userName
	 * @return
	 */
	public static boolean matchUserName(String userName) {
		if(StringUtils.isBlank(userName))
			return false;
		if(userName.matches("^[0-9]*$"))
			return false;
		Pattern p = Pattern.compile(USER_NAME);  
		Matcher m = p.matcher(userName);
		return m.matches();
		
	}
	
	//验证用户名是否合法
	public static boolean matchNickname(String nickname){
		Pattern p = Pattern.compile(NICK_NAME);  
		Matcher m = p.matcher(nickname);
		return m.matches();
		
	}
	/**
	 * 正则匹配电话
	 * @param mobile
	 * @return
	 */
	public static boolean matchMobile(String mobile){
		Pattern p = Pattern.compile(MOBILE);  
		Matcher m = p.matcher(mobile);  
		return m.matches();
	}
	
	/**
	 * 获取6个随机数
	 * @return
	 */
	public static String build6Random(){
		return String.valueOf((int)((Math.random()*9+1)*100000));
	}
	/**
	 * 获取4个随机数
	 * @return
	 */
	public static String build4Random(){
		return String.valueOf((int)((Math.random()*9+1)*1000));
	}
	public static double get3Double(double a){  
	    DecimalFormat df=new DecimalFormat("0.###"); 
	    df.setRoundingMode(RoundingMode.HALF_UP);
	    return new Double(df.format(a).toString());  
	} 
	public static double get4Double(double a){  
	    DecimalFormat df=new DecimalFormat("0.####"); 
	    df.setRoundingMode(RoundingMode.HALF_UP);
	    return new Double(df.format(a).toString());  
	} 
	public static double get2Double(double a){  
	    DecimalFormat df=new DecimalFormat("0.##");  
	    df.setRoundingMode(RoundingMode.HALF_UP);
	    return new Double(df.format(a).toString());  
	} 
	public static double get2DoubleByDown(double a){  
		DecimalFormat df=new DecimalFormat("0.##");  
		df.setRoundingMode(RoundingMode.DOWN);
		return new Double(df.format(a).toString());  
	} 
	public static String hiddenMobile(String mobile){
		return hidePhoneNumber(mobile, 3, 5);
	}
	/**
	 * 隐藏姓名
	 * 
	 * @param name
	 *            姓名
	 * @param offset
	 *            第几个开始
	 * @return
	 */
	public static String hideName(String name, int offset) {
		StringBuilder targetName = new StringBuilder(name.substring(0, offset));
		for (int index = 0; index < name.length() - offset; index++) {
			targetName.append("*");
		}
		return targetName.toString();
	}
	
	/**
	 * 隐藏身份证号
	 * 
	 * @param idCard
	 * @return
	 */
	public static String hideIdCard(String idCard) {
		if (idCard.length() == 18) {
			return idCard.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1*****$2");
		} else if (idCard.length() == 15) {
			return idCard.replaceAll("(\\d{4})\\d{7}(\\w{4})", "$1*****$2");
		}
		return idCard;
	}
	
	/**
	 * 隐藏手机号
	 * 
	 * @param mobile
	 * @param offset
	 * @param count
	 * @return
	 * @version 2016年11月3日
	 * @author meme
	 */
	public static String hidePhoneNumber(Object mobile, int offset, int count) {
		String mobileStr = mobile.toString();
		if (StringUtils.isBlank(mobileStr) || mobileStr.length() < 11){
			return mobileStr;
		}
		
		if (offset + count > 11 || offset < 0 || count <= 0) {
			throw new IllegalArgumentException("Argument offset or count wrong!");
		}
		
		return mobileStr.substring(0, offset) 
			+ new String(new char[count]).replace('\0', '*')
			+ mobileStr.substring(offset + count);
	}
	
	/**
	 * int判定
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str){ 
		   Pattern pattern = Pattern.compile("[0-9]*"); 
		   Matcher isNum = pattern.matcher(str);
		   if( !isNum.matches() ){
		       return false; 
		   } 
		   return true; 
	}
	
	/**
	 * String非空判断
	 * @param _value
	 * @return
	 */
	public static boolean stringNotEmpty(String _value){
		if(_value!=null && !_value.equals("") && !_value.equals("null")){
			return true;
		}
		return false;
	}
	
	/**
	 * 格林尼治时间转成北京时间。
	 * @param gmtTime 格林尼治时间
	 * @return 
	 * @throws ParseException 
	 */
	public static Date toBeijingTime(String gmtTime) throws ParseException {
		String pattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat gmtFormat = new SimpleDateFormat(pattern);
		TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
		gmtFormat.setTimeZone(gmtTimeZone);
		return gmtFormat.parse(gmtTime);
	}
	
	/**
	 * Object非空判断
	 * @param object
	 * @return
	 */
	public static boolean objectNotEmpty(Object object){
		if(object!=null && !object.equals("") && !object.equals("null")){
			return true;
		}
		return false;
		
	}
	
	/**
	 * double类型冒泡排序
	 * @param a
	 */
    public static void bubbleSort(double[] a)
    {
    	double temp = 0;
        for (int i = a.length - 1; i > 0; --i)
        {
            for (int j = 0; j < i; ++j)
            {
                if (a[j + 1] < a[j])
                {
                    temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }
    
	public static String hideDualPointCharInContentWithCharX(String content) {
		char[] contentArray = content.toCharArray();
		for (int i = 1; i < contentArray.length; i+=2) {
			contentArray[i] = 'X';
		}
		return new String(contentArray);
	}
	
	
	public static long yuanToFen(double yuan){
		BigDecimal d = new BigDecimal(Double.toString(yuan));
		return d.multiply(new BigDecimal(Double.toString(100))).longValue();
	}
	public static double fenToyuan(double fen){
		BigDecimal d1 = new BigDecimal(Double.toString(fen));
		return d1.divide(new BigDecimal(Double.toString(100))).doubleValue();
	}
	
}
